<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>宜鲜生</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <!-- load css -->
    <link rel="stylesheet" type="text/css" href="../css/bootstrap.min.css?v=v3.3.7" media="all">
    <link rel="stylesheet" type="text/css" href="../css/font/iconfont.css?v=1.0.0" media="all">
    <link rel="stylesheet" type="text/css" href="../css/layui.css?v=1.0.9" media="all">
    <link rel="stylesheet" type="text/css" href="../css/main.css?v1.3.4" media="all">

    <link rel="stylesheet" type="text/css" href="../css/autocomplete.css" media="all">

</head>
<body>
    <div class="container-fluid larry-wrapper">
        <div class="row">
            <div class="col-xs-12 col-sm-12 col-md-12">
                <!--头部搜索-->
                <section class="panel panel-padding">
                    <form class="layui-form" action="/education/admin/sellerEnter">
                        <div class="layui-form">
                            <div class="layui-inline">
                                <select name="_query.area_id" id="areaId2">
                                    <option value="">区县</option>
                                </select>
                            </div>

                            <div class="layui-inline">
                                <div class="layui-input-inline">
                                    <input class="layui-input" name="_query.keyword" placeholder="关键字">
                                </div>
                            </div>
                            <div class="layui-inline">
                                <button lay-submit class="layui-btn" lay-filter="search">查找</button>
                            </div>

                        </div>
                    </form>
                </section>

                <!--列表-->
                <section class="panel panel-padding">

                    <div class="group-button">
                        <button class="layui-btn layui-btn-small modal-catch" beforeFun="beforeAdd"
                                data-params='{"content":".add-subcat", "title":"添加","type":"1", "full":"true", "maxmin":"true","data":"url=/education/admin/sellerEnter/save"}'>
                            <i class="iconfont">&#xe649;</i> 添加
                        </button>
                        <!--<button class="layui-btn layui-btn-small" onclick="viewDetail();">
                            <i class="iconfont">&#xe653;</i>编辑
                        </button>-->
                        <button class="layui-btn layui-btn-small layui-btn-danger ajax-all" data-name="checkbox" data-params='{"url": "/education/admin/sellerEnter/delete"}'>
                            <i class="iconfont">&#xe626;</i> 删除
                        </button>

                       <!-- <button class="layui-btn layui-btn-small layui-btn-normal" onclick="viewAudit();">
                            <i class="iconfont">&#xe63f;</i> 审核
                        </button>-->

                    </div>
                    <div id="list" class="layui-form"></div>

                    <div class="text-right" id="page"></div>
                </section>
            </div>
        </div>
    </div>
    <div class="add-subcat">
        <form class="layui-form layui-form-pane" action="/education/admin/sellerEnter/save" id="form1">
            <input type="hidden" name="id">
            <div class="layui-form-item">
                <label class="layui-form-label">场馆名称</label>
                <div class="layui-input-inline">
                    <input type="text" name="site_name" required jq-verify="required" class="layui-input ">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label" style="width:200px">场馆照片</label>
                <div class="layui-input-block">
                    <input type="file" name="logo" class="layui-upload-file">
                    <input type="hidden" name="site_img">
                </div>
                <div class="layui-input-block"><div class="imgbox" style="width: 800px;"></div></div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">联系人</label>
                <div class="layui-input-inline">
                    <input type="text" name="owner" class="layui-input ">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">联系电话</label>
                <div class="layui-input-inline">
                    <input type="number" name="tel" class="layui-input ">
                </div>
            </div>
            <!--<div class="layui-form-item">
                <label class="layui-form-label">开业时间</label>
                <div class="layui-input-inline">
                    <input class="layui-input" name="opentime" onclick="layui.laydate({elem: this})">
                </div>
            </div>-->
            <!--<div class="layui-form-item">
                <label class="layui-form-label">所属区域</label>
                <div class="layui-input-inline">
                    <select name="areaid" jq-verify="required" jq-error="请选择地区" lay-filter="area">
                        <option value=""></option>
                    </select>
                </div>
                <div class="layui-input-inline">
                    <select name="street" jq-verify="required" jq-error="请选择地区" lay-filter="street">
                        <option value=""></option>
                    </select>
                </div>
            </div>-->

            <div class="layui-form-item">
                <label class="layui-form-label">所属区域</label>
                <div class="layui-input-inline">
                    <select name="area_id" jq-verify="required" jq-error="请选择地区" id="areaId">
                        <option value=""></option>
                        <optgroup label=""></optgroup>
                    </select>
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">详细地址</label>
                <div class="layui-input-inline">
                    <input type="text" name="address" required jq-verify="required" jq-error="请输入地址"
                           id="address" class="layui-input ">
                </div>
                <a class="layui-btn" onclick="beforeChoice();">
                    <i class="iconfont">&#xe649;</i> 选择
                </a>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">所属类别</label>
                <div class="layui-input-inline">
                    <select name="site_type" jq-verify="required" jq-error="请选择" id="siteType">
                        <option value=""></option>
                    </select>
                </div>
            </div>

            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button class="layui-btn" jq-submit jq-filter="submit">立即提交</button>
                    <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                </div>
            </div>
        </form>
    </div>

    <div class="edit-subcat">
        <form class="layui-form layui-form-pane" action="/education/admin/sellerEnter/audit" id="form2">
            <div class="layui-form-item">
                <label class="layui-form-label">状态</label>
                <div class="layui-input-inline">
                    <select name="status" required jq-verify="required" jq-error="请选择"
                            class="layui-input ">
                        <option value="">请选择</option>
                        <option value="1">通过</option>
                        <option value="2">驳回</option>
                    </select>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">说明</label>
                <div class="layui-input-block">
                    <textarea name="audit_desc"  class="layui-textarea"></textarea>
                </div>
            </div>

            <div id="auditInfo">
                <div class="layui-form-item">
                    <label class="layui-form-label">审核人</label>
                    <div class="layui-input-block">
                        <input type="text" name="audit_username"  class="layui-input ">
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">审核时间</label>
                    <div class="layui-input-block">
                        <input type="text" name="audit_time" class="layui-input ">
                    </div>
                </div>
            </div>


            <!--<div class="layui-form-item">
                <div style="color: #ff4e1b">
                    提示：关联用户会取消之前已关联的用户
                </div>
            </div>-->
            <div class="layui-form-item" id="submit">
                <div class="layui-form-item">
                    <div class="layui-input-block">
                        <button class="layui-btn" jq-submit jq-filter="submit" before-fun="beforeSubmit();">立即提交</button>
                        <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                    </div>
                </div>
            </div>
        </form>
    </div>
</body>
<!--#include file="seller-enter-list.html"-->
<script src="../js/layui/layui.js"></script>
<!--#include file="common/version.html"-->
<script type="text/javascript" src="../js/lib/jquery-1.11.2.min.js"></script>
<script type="text/javascript" src="../js/lib/rest.js"></script>
<script>

    layui.use('user_list');

    layui.use('upload', function(){
        var box;
        //上传文件设置
        layui.upload({
            elem:'.layui-upload-file',
            url: '/education/api/image/site',
            before: function(input) {
                box = $(input).parent('form').parent('div').parent('.layui-input-block');
                if (box.next('div').length > 0) {
                    box.next('div').html('<div class="imgbox"><p>上传中...</p></div>');
                } else {
                    box.after('<div class="layui-input-block"><div class="imgbox"><p>上传中...</p></div></div>');
                }
            },
            success: function(res) {
                if (res.code == 200) {
                    box.next('div').find('div.imgbox').html('<img src="/education' + res.data[0].url + '" alt="..." class="img-thumbnail">');
                    box.find('input[type=hidden]').val(res.data[0].url);
                } else {
                    box.next('div').find('p').html('上传失败...')
                }
            }
        });
    });


    function initData(){
        restPost('/education/api/dict', {type:'site.type'}, function(rs){
            var arr = ['<option value=""></option>'];
            if(rs.code == 200){
                $.each(rs.data, function(i, n)
                {
                    arr.push('<option value="{v}">{n}</option>'.replace('{v}', n.code).replace('{n}', n.value));
                });
            }
            $('#siteType').html(arr.join(''));
        });

        restPost('/education/admin/area/selectData', null, function(rs){
            var arr = ['<option value=""></option>'];
            if(rs.status == 200){
                $.each(rs.data, function(i, n)
                {
                    //arr.push('<option value="{v}">{n}</option>'.replace('{v}', n.id).replace('{n}', n.name));
                    arr.push('<optgroup label="'+n['city']+'">');//</optgroup>'
                    for(var j=0; j< n['list'].length; j++){
                        var d = n['list'][j];
                        arr.push('<option value="{v}">{n}</option>'.replace('{v}', d.id).replace('{n}', d.name));
                    }
                    arr.push('</optgroup>');
                });
            }
            $('#areaId').add('#areaId2').html(arr.join(''));
        });

    }

    function viewAudit(ids){
        if(!ids){
            var checked = $("input[name=checkbox]:checked");
            if(checked.length != 1){
                layer.msg('请选择一项待修改的数据');
                return;
            }
            ids = checked.val();
        }

        $('#form2').attr('action', "/education/admin/sellerEnter/audit/" + ids);

        restPost('/education/admin/sellerEnter/view/' + ids, null, function(rs){
            if(rs.status == 200){
                var d = rs.data;
                $('select[name=status]', '#form2').val(d.status);
                $('textarea[name=audit_desc]', '#form2').text(d.audit_desc||'');
                if(d.status != 0){
                    $('input[name="audit_username"]', '#form2').val(d.audit_username);
                    console.log($('input[name="audit_username"]', '#form2').val())

                    $('input[name="audit_time"]', '#form2').val(d.audit_time);
                    $('input,select,textarea','#form2').attr('readonly',true);
                    $('#submit').hide();
                    $('#auditInfo').show();
                } else {
                    $('input,select,textarea','#form2').removeAttr('readonly');
                    $('#auditInfo').hide();
                    $('#submit').show();
                }
            }
        });

        var index = layer.open({
            type: 1,
            title:'审核',
            area: ['600px', '400px'],
            content: $('.edit-subcat')
        });
    }

    function beforeSubmit(){

    }

    function viewDetail(ids){
        if(!ids){
            var checked = $("input[name=checkbox]:checked");
            if(checked.length != 1){
                layer.msg('请选择一项待修改的数据');
                return;
            }
            ids = checked.val();
        }

        var index = layer.open({
            type: 1,
            title:'编辑',
            content: $('.add-subcat')
        });
        layer.full(index);
        $('.imgbox').children().remove();
        $('#form1').attr('action', '/education/admin/sellerEnter/update');
        restGet('/education/admin/sellerEnter/view/' + ids, null, $('#form1'), function(rs){console.log(rs)
            if(rs.code == 200){
                var str =  '<img src="/education{src}" alt="..." class="img-thumbnail">';
                var imgDiv = $('input[name=img]').parent().next().children('.imgbox');
                imgDiv.append(str.replace('{src}', rs.data.img));
            }
        })
    }

    function beforeAdd(obj){
        $('#areaText').html('');
        $('.imgbox').children().remove();
    }

    function beforeUpdate(obj, id){
        var ids = id || _getIds();
        var p = $(obj).attr('data-params');
        $(obj).attr('data-params', p.replace('{ids}', ids));
        restGet('/education/admin/sellerEnter/view/' + ids, null, $('#form1'), function(rs){console.log(rs)
            //清空原有数据
            $('.imgbox',$('#form1')).remove();

            //设置图片
            $.each(d, function(i, n){
                if(n && new String(n).indexOf('/files/upload') > -1){
                    _showImg(i, n);
                }
            });

            refreshImg();
        })
    }

    function _getIds(){
        var ids = '';
        var arr = $("[name=checkbox]:checked");
        if (arr.length != 1) {
            layer.msg('请选择一条需要查看的记录');
            return;
        } else {
            ids = arr.val();
        }
        return ids;
    }

    var _showImg = function(k, q){
        var arr = q.split(',');
        var content ='';
        for(var i=0; i< arr.length; i++){
            var _url = arr[i];
            var str =  '<img name="file" src="{src}" class="img-thumbnail" style="height:200px;">';
            str = str.replace(/{src}/g, '/education'+_url);
            str = '<span class="layer-photos-demo">' + str + '</span>';
            str += '<a style="cursor: pointer;margin-right: 50px;" onclick="delImg(this)"><span class="layui-icon" style="color: red;font-size: xx-large;vertical-align:top;">&#x1006;</span></a>'
            content += '<span>' + str + '</span>';
        }
        var rs = '<div class="imgbox" style="height: 200px;">'+content+'</div>';
        $('input[name='+k+']').parent().after(rs);
    };

    function delImg(obj){
        var input = $(obj).parent().parent().prev().find('input[type=hidden]');
        if($(obj).parent().parent().children('span').length == 1){
            input.val('');
            input.prev().val('');//两张图片的处理
            $(obj).parent().parent().remove();
        } else {
            var v = $(obj).prev().find('img').attr('src').replace('/education', '');
            //先处理两张图片的情况
            if(input.prev('input').length >0){
                if(v == input.prev('input').val()){
                    input.prev('input').val('');
                }else{
                    input.val('');
                }
            } else {
                var s = input.val().replace(','+ v, '')
                        .replace(v +',', '');
                input.val(s);
            }
            $(obj).parent().remove();
        }
    }

    function refreshImg(){
        layui.use('layer', function(){
            var layer = layui.layer;
            layer.photos({
                photos: '.layer-photos-demo'
                ,anim: 5 //0-6的选择，指定弹出图片动画类型，默认随机（请注意，3.0之前的版本用shift参数）
            });
        });
    }

    function beforeChoice(){
        layer.open({
            type: 2,
            content: "map.html",
            "title": "选择地点",
            area: ['550px', '480px'], "maxmin":"true"
        });
    }
</script>

</html>